<?php
namespace app\crontab\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class DeleteFileTask extends Command
{
    protected function configure()
    {
        $this->setName('task')
            ->setDescription('每天执行的定时计划,删除多余图片');
    }

    protected function execute(Input $input, Output $output)
    {
        //每天晚上12点删除指定文件下的图片
        $this->deleteImages();
    }

    /**
     * 删除upload/code/下的所有图片
     */
    private function deleteImages()
    {
        $path   = ROOT_PATH . 'public' . DS . "upload/code";
        $delDir = false;
        if (is_array($path)) {
            foreach ($path as $subPath) {
                delDirAndFile($subPath, $delDir);
            }

        }
        if (is_dir($path)) {
            $handle = opendir($path);
            if ($handle) {
                while (false !== ($item = readdir($handle))) {
                    if ($item != "." && $item != "..") {
                        is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
                    }

                }
                closedir($handle);
                if ($delDir) {
                    return rmdir($path);
                }

            }
        } else {
            if (file_exists($path)) {
                return unlink($path);
            } else {
                return false;
            }
        }
        clearstatcache();
    }

}
